/* Copyright (c) 2010 Sven Weidauer
 
 Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include <Platform/Events.h>

#include <windows.h>

bool MPCreateEvent( MPEvent *outEvent, MPEventParameters parameters )
{
    if (!outEvent) return false;
    
    BOOL initial = (parameters & MP_EVENT_CREATE_SIGNALED) == MP_EVENT_CREATE_SIGNALED;
    BOOL manualReset = (parameters & MP_EVENT_MANUAL_RESET) == MP_EVENT_MANUAL_RESET;
    
    HANDLE event = CreateEvent( 0, manualReset, initial, 0 );
    if (!event) return false;
    
    *outEvent = (MPEvent)event;
    return true;
}

bool MPSetEvent( MPEvent event )
{
    return SetEvent( event );
}

bool MPResetEvent( MPEvent event )
{
    return ResetEvent( event );
}

void MPRetainEvent( MPEvent event )
{
    // TODO: What to do on windows?
}

bool MPReleaseEvent( MPEvent *inoutEvent )
{
    if (!(inoutEvent && *inoutEvent)) return false;
    CloseHandle( *inoutEvent );
    *inoutEvent = 0;
    return true;
}

MPWaitResult MPWaitForAnyEvent( unsigned count, MPEvent *events, int timeout )
{
    DWORD time = timeout;
    if (MP_TIMEOUT_FOREVER == timeout) time = INFINITE;
    
    DWORD result = MsgWaitForMultipleObjects( count, (HANDLE *)events, FALSE, time, QS_ALLINPUT );
    
    if (WAIT_OBJECT_0 <= result && result < WAIT_OBJECT_0 + count) {
        unsigned which = result - WAIT_OBJECT_0;
        return (MPWaitResult)(MP_WAIT_EVENT_0 + which);
    } else if (WAIT_OBJECT_0 + count == result) {
        return MP_WAIT_SYSTEM_EVENT;
    } else if (WAIT_TIMEOUT == result) {
        return MP_WAIT_TIMEOUT;
    } else return MP_WAIT_ERROR;
}

MPWaitResult MPWaitForAllEvents( unsigned count, MPEvent *events, int timeout )
{
    DWORD time = timeout;
    if (MP_TIMEOUT_FOREVER == timeout) time = INFINITE;
    
    DWORD result = MsgWaitForMultipleObjects( count, (HANDLE *)events, TRUE, time, QS_ALLINPUT );
    
    if ((WAIT_OBJECT_0 <= result && result < WAIT_OBJECT_0 + count) || (WAIT_ABANDONED_0 <= result && result < WAIT_ABANDONED_0 + count)) {
        return MP_WAIT_FINNISHED;
    } else if (WAIT_OBJECT_0 + count == result) {
        return MP_WAIT_SYSTEM_EVENT;
    } else if (WAIT_TIMEOUT == result) {
        return MP_WAIT_TIMEOUT;
    } else return MP_WAIT_ERROR;
}

MPWaitResult MPWaitForEvent( MPEvent event, int timeout )
{
    return MPWaitForAnyEvent( 1, &event, timeout );
}
